package lily.phs.healthedu;


import java.util.Calendar;
import lily.phs.urh.R;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;


public class DateChooseView extends RelativeLayout{
	
	Gallery gYear,gMonth,gDay;
	private int year,month,day;
	

	private int dayOfMonth;
	Context myContext;
	TextView tv;

	public DateChooseView(Context context) {
		super(context);
		myContext=context;
		LayoutInflater factory = LayoutInflater.from(context);
        View v = factory.inflate(R.layout.datechoose_layout, null);
        tv=(TextView)v.findViewById(R.id.tv_date);
        tv.setTextColor(Color.WHITE);
        FrameLayout flYear = (FrameLayout) v.findViewById(R.id.flyear);
        FrameLayout flMonth = (FrameLayout) v.findViewById(R.id.flmonth);
        FrameLayout flDay = (FrameLayout) v.findViewById(R.id.flday);
        RocateLayout rlYear = new RocateLayout(context);
        RocateLayout rlMonth = new RocateLayout(context);
        RocateLayout rlDay = new RocateLayout(context);
        flYear.addView(rlYear);
        flMonth.addView(rlMonth);
        flDay.addView(rlDay);
        gYear=new Gallery(context);
        gMonth=new Gallery(context);
        gDay=new Gallery(context);
        gYear.setBackgroundResource(R.drawable.bg5);
        gMonth.setBackgroundResource(R.drawable.bg5);
        gDay.setBackgroundResource(R.drawable.bg5);
        rlYear.addView(gYear);
        rlMonth.addView(gMonth);
        rlDay.addView(gDay);
        Calendar c=Calendar.getInstance();
        year=c.get(Calendar.YEAR);
        month=c.get(Calendar.MONTH)+1;
        day=c.get(Calendar.DAY_OF_MONTH);
        System.out.println(year+" "+month+" "+day);
        gYear.setAdapter(new ImageAdapter(context,imYear)); 
        gYear.setSpacing(20);
        gMonth.setAdapter(new ImageAdapter(context,imMonth)); 
        gMonth.setSpacing(20);
        gDaySetAdapter(context);
        gYear.setSelection(99-(year-1920));
        gMonth.setSelection(12-month);
        gDay.setSelection(dayOfMonth-day);
        //gYear.setSelection(position)
        this.addView(v);   
        
        gYear.setOnItemSelectedListener(new OnItemSelectedListener(){

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				year=1920+(99-arg2);
				if(month==2){
					gDaySetAdapter(myContext);
					gDay.setSelection(dayOfMonth-day);
				}
				tv.setText(dateString());
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
        	
        });
        
        gMonth.setOnItemSelectedListener(new OnItemSelectedListener(){

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				month=12-arg2;
				gDaySetAdapter(myContext);
				gDay.setSelection(dayOfMonth-day);
				tv.setText(dateString());
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
        	
        });
        
        gDay.setOnItemSelectedListener(new OnItemSelectedListener(){

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				day=dayOfMonth-arg2;
				tv.setText(dateString());
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
        	
        });
        tv.setText(dateString());
	}
	
	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public int getMonth() {
		return month;
	}

	public void setMonth(int month) {
		this.month = month;
	}

	public int getDay() {
		return day;
	}

	public void setDay(int day) {
		this.day = day;
	}
	
	boolean IfLeap(int y)//�ж��Ƿ����� 
	{ 
		if(y%400==0)return true; 
		if(y%100==0)return false; 
		if(y%4==0)return true; 
		return false; 
	} 

	
	public void gDaySetAdapter(Context context){
		
		if(month==4||month==6||month==9||month==11){
			gDay.setAdapter(new ImageAdapter(context,imDay30)); 
			dayOfMonth=30;
		}
		else if(month==2){
			if(IfLeap(year)){
				gDay.setAdapter(new ImageAdapter(context, imDay29));
				dayOfMonth=29;
			}
			else{
				gDay.setAdapter(new ImageAdapter(context,imDay28)); 
				dayOfMonth=28;
			}
		}
		else{
			gDay.setAdapter(new ImageAdapter(context,imDay31)); 
			dayOfMonth=31;
		}
	    gDay.setSpacing(20);
	}
	
	public class ImageAdapter extends BaseAdapter {
	    int mGalleryItemBackground;
	    private Context mContext;  
	    private int image[];
	    public ImageAdapter(Context c,int i[]) {
	        mContext = c; 
	        this.image=i;
	    }

	    public int getCount() {
	        return image.length;
	    } 
	    public Object getItem(int position) {
	        return position;
	    }

	    public long getItemId(int position) {
	        return position;
	    }

	    public View getView(int position, View paramView, ViewGroup parent) {
	    	ImageView i = new ImageView(mContext);
			i.setImageResource(image[position]);
			i.setScaleType(ImageView.ScaleType.FIT_XY);
			i.setLayoutParams(new Gallery.LayoutParams(40, 60));
			i.setBackgroundResource(mGalleryItemBackground);

			return i;
	    }  
	}
	
	private int[] imMonth = {
					R.drawable.num12, R.drawable.num11, R.drawable.num10,

					R.drawable.num09, R.drawable.num08, R.drawable.num07,
					R.drawable.num06, R.drawable.num05, R.drawable.num04,
					R.drawable.num03, R.drawable.num02, R.drawable.num01,
	};
	
	private int[] imYear = {

					R.drawable.num19, R.drawable.num18, R.drawable.num17,
					R.drawable.num16, R.drawable.num15, R.drawable.num14,
					R.drawable.num13, R.drawable.num12, R.drawable.num11,
					R.drawable.num10,

					R.drawable.num09, R.drawable.num08, R.drawable.num07,
					R.drawable.num06, R.drawable.num05, R.drawable.num04,
					R.drawable.num03, R.drawable.num02, R.drawable.num01,
					R.drawable.num00, 
					
					R.drawable.num99, R.drawable.num98, R.drawable.num97,
					R.drawable.num96, R.drawable.num95, R.drawable.num94,
					R.drawable.num93, R.drawable.num92, R.drawable.num91,
					R.drawable.num90,
					
					R.drawable.num89, R.drawable.num88, R.drawable.num87,
					R.drawable.num86, R.drawable.num85, R.drawable.num84,
					R.drawable.num83, R.drawable.num82, R.drawable.num81,
					R.drawable.num80,
					
					R.drawable.num79, R.drawable.num78, R.drawable.num77,
					R.drawable.num76, R.drawable.num75, R.drawable.num74,
					R.drawable.num73, R.drawable.num72, R.drawable.num71,
					R.drawable.num70,
					
					R.drawable.num69, R.drawable.num68, R.drawable.num67,
					R.drawable.num66, R.drawable.num65, R.drawable.num64,
					R.drawable.num63, R.drawable.num62, R.drawable.num61,
					R.drawable.num60,
					
					R.drawable.num59, R.drawable.num58, R.drawable.num57, R.drawable.num56,
					R.drawable.num55, R.drawable.num54, R.drawable.num53,
					R.drawable.num52, R.drawable.num51, R.drawable.num50,
					
					R.drawable.num49, R.drawable.num48, R.drawable.num47,
					R.drawable.num46, R.drawable.num45, R.drawable.num44,
					R.drawable.num43, R.drawable.num42, R.drawable.num41,
					R.drawable.num40,
					
					R.drawable.num39, R.drawable.num38, R.drawable.num37,
					R.drawable.num36, R.drawable.num35, R.drawable.num34,
					R.drawable.num33, R.drawable.num32, R.drawable.num31,
					R.drawable.num30,
					
					R.drawable.num29, R.drawable.num28, R.drawable.num27,
					R.drawable.num26, R.drawable.num25, R.drawable.num24,
					R.drawable.num23, R.drawable.num22, R.drawable.num21,
					R.drawable.num20,
					
	};
	
	private int[] imDay29 = {
			
			R.drawable.num29, R.drawable.num28, R.drawable.num27,
			R.drawable.num26, R.drawable.num25, R.drawable.num24,
			R.drawable.num23, R.drawable.num22, R.drawable.num21,
			R.drawable.num20,
			
			R.drawable.num19, R.drawable.num18, R.drawable.num17,
			R.drawable.num16, R.drawable.num15, R.drawable.num14,
			R.drawable.num13, R.drawable.num12, R.drawable.num11,
			R.drawable.num10,
			
			R.drawable.num09, R.drawable.num08, R.drawable.num07,
			R.drawable.num06, R.drawable.num05, R.drawable.num04,
			R.drawable.num03, R.drawable.num02, R.drawable.num01,
			
	};
	
	private int[] imDay28 = {
			
			 R.drawable.num28, R.drawable.num27,
			R.drawable.num26, R.drawable.num25, R.drawable.num24,
			R.drawable.num23, R.drawable.num22, R.drawable.num21,
			R.drawable.num20,
			
			R.drawable.num19, R.drawable.num18, R.drawable.num17,
			R.drawable.num16, R.drawable.num15, R.drawable.num14,
			R.drawable.num13, R.drawable.num12, R.drawable.num11,
			R.drawable.num10,
			
			R.drawable.num09, R.drawable.num08, R.drawable.num07,
			R.drawable.num06, R.drawable.num05, R.drawable.num04,
			R.drawable.num03, R.drawable.num02, R.drawable.num01,
			
	};
	
	private int[] imDay30 = {
			R.drawable.num30, R.drawable.num29,
			
			 R.drawable.num28, R.drawable.num27,
			R.drawable.num26, R.drawable.num25, R.drawable.num24,
			R.drawable.num23, R.drawable.num22, R.drawable.num21,
			R.drawable.num20,
			
			R.drawable.num19, R.drawable.num18, R.drawable.num17,
			R.drawable.num16, R.drawable.num15, R.drawable.num14,
			R.drawable.num13, R.drawable.num12, R.drawable.num11,
			R.drawable.num10,
			
			R.drawable.num09, R.drawable.num08, R.drawable.num07,
			R.drawable.num06, R.drawable.num05, R.drawable.num04,
			R.drawable.num03, R.drawable.num02, R.drawable.num01,
			
	};
	
	private int[] imDay31 = {
			R.drawable.num31,R.drawable.num30, R.drawable.num29,
			
			 R.drawable.num28, R.drawable.num27,
			R.drawable.num26, R.drawable.num25, R.drawable.num24,
			R.drawable.num23, R.drawable.num22, R.drawable.num21,
			R.drawable.num20,
			
			R.drawable.num19, R.drawable.num18, R.drawable.num17,
			R.drawable.num16, R.drawable.num15, R.drawable.num14,
			R.drawable.num13, R.drawable.num12, R.drawable.num11,
			R.drawable.num10,
			
			R.drawable.num09, R.drawable.num08, R.drawable.num07,
			R.drawable.num06, R.drawable.num05, R.drawable.num04,
			R.drawable.num03, R.drawable.num02, R.drawable.num01,
			
	};
	
	public String dateString(){
		String str="";
		str+=year;
		if(month<10){
			str+="-0"+month;
		}
		else{
			str+="-"+month;
		}
		if(day<10){
			str+="-0"+day;
		}
		else{
			str+="-"+day;
		}
		return str;
	}

}
